
#define  Align4(S)				( ((S)/4 + 1)*4 ) 

#define Small_Stream	400 
#define NLevels			100


/*=================================================================================================

					  M E M O R Y -- E N C O D E R

=================================================================================================*/

#define	 ID_ENCODER_Cnt1			0
#define	 ID_ENCODER_BitLine			1
#define	 ID_ENCODER_BitLine1		2	
#define	 ID_ENCODER_Sign			3
#define  ID_ENCODER_SortDelta		4
#define	 ID_ENCODER_NewIndex_X		5 
#define	 ID_ENCODER_NewIndex_Y		6 
#define	 ID_ENCODER_LevelQ			7 
#define	 ID_ENCODER_QuantDelta		8 
#define	 ID_ENCODER_Probability		9 
#define	 ID_ENCODER_Quart_Buffer	10 
#define	 ID_ENCODER_Temp_Line1		11 
#define	 ID_ENCODER_Temp_Line2		12 
#define	 ID_ENCODER_Filter_Params	13 
//_____________________________________
#define	 ID_ENCODER_END				14	

#define	 SIZE_ENCODER_Cnt1(W,H)				( (((W)*(H)/2)/UBoundCNL + 1)*4 )		
#define	 SIZE_ENCODER_BitLine(W,H)			( Align4(((W)*(H)/2)/8 + 1 + 256) )		
#define	 SIZE_ENCODER_BitLine1(W,H)			( Align4(((W)*(H)/2)/8 + 1) )		
#define	 SIZE_ENCODER_Sign(W,H)				( Align4(Max_(Small_Stream/8,(((W)*(H)/2)/UBoundCNL + 1)/8 + 1)) )
#define	 SIZE_ENCODER_SortDelta(W,H)		( Align4(((W)*(H)/2) + 1) )		
#define	 SIZE_ENCODER_NewIndex_X(W,H)		( 0x4000 )		
#define	 SIZE_ENCODER_NewIndex_Y(W,H)		( 0x4000 )		
#define	 SIZE_ENCODER_LevelQ(W,H)			( (NLevels*4) )		
#define	 SIZE_ENCODER_QuantDelta(W,H)		( Align4(W*H) )	 		
#define	 SIZE_ENCODER_Probability(W,H)		( Align4(W*H*2) )	
#define	 SIZE_ENCODER_Quart_Buffer(W,H)		( Align4(W*H) )	 
#define	 SIZE_ENCODER_Temp_Line1(W,H)		( Align4(W+2) )
#define	 SIZE_ENCODER_Temp_Line2(W,H)		( Align4(W+2+64) )
#define	 SIZE_ENCODER_Filter_Params(W,H)	( 4*(32+1) )




#define	ENCODER_MEMORY_SIZE(W, H) ( (ID_ENCODER_END*4)					+ \
									(SIZE_ENCODER_Cnt1(W,H))			+ \
									(SIZE_ENCODER_BitLine(W,H))			+ \
									(SIZE_ENCODER_BitLine1(W,H))		+ \
									(SIZE_ENCODER_Sign(W,H))			+ \
									(SIZE_ENCODER_SortDelta(W,H))		+ \
									(SIZE_ENCODER_NewIndex_X(W,H))		+ \
									(SIZE_ENCODER_NewIndex_Y(W,H))		+ \
									(SIZE_ENCODER_LevelQ(W,H))			+ \
									(SIZE_ENCODER_QuantDelta(W,H))	 	+ \
									(SIZE_ENCODER_Probability(W,H))		+ \
									(SIZE_ENCODER_Quart_Buffer(W,H))	+ \
									(SIZE_ENCODER_Temp_Line1(W,H))		+ \
									(SIZE_ENCODER_Temp_Line2(W,H))		+ \
									(SIZE_ENCODER_Filter_Params(W,H))	) \
//_________________________________________________________________________
								
									  


#define ENCODER_MEMORY_INIT(Mem, W, H)	((int*)Mem)[0]	= ID_ENCODER_END*4;									\
										((int*)Mem)[1]	= ((int*)Mem)[0]  + SIZE_ENCODER_Cnt1(W,H);			\
										((int*)Mem)[2]	= ((int*)Mem)[1]  +	SIZE_ENCODER_BitLine(W,H);		\
										((int*)Mem)[3]	= ((int*)Mem)[2]  +	SIZE_ENCODER_BitLine1(W,H);		\
										((int*)Mem)[4]	= ((int*)Mem)[3]  +	SIZE_ENCODER_Sign(W,H);			\
										((int*)Mem)[5]	= ((int*)Mem)[4]  +	SIZE_ENCODER_SortDelta(W,H);	\
										((int*)Mem)[6]	= ((int*)Mem)[5]  +	SIZE_ENCODER_NewIndex_X(W,H);	\
										((int*)Mem)[7]	= ((int*)Mem)[6]  +	SIZE_ENCODER_NewIndex_Y(W,H);	\
										((int*)Mem)[8]	= ((int*)Mem)[7]  + SIZE_ENCODER_LevelQ(W,H);		\
										((int*)Mem)[9]	= ((int*)Mem)[8]  + SIZE_ENCODER_QuantDelta(W,H);	\
										((int*)Mem)[10] = ((int*)Mem)[9]  + SIZE_ENCODER_Probability(W,H);	\
										((int*)Mem)[11] = ((int*)Mem)[10] + SIZE_ENCODER_Quart_Buffer(W,H);	\
										((int*)Mem)[12] = ((int*)Mem)[11] + SIZE_ENCODER_Temp_Line1(W,H);	\
										((int*)Mem)[13] = ((int*)Mem)[12] + SIZE_ENCODER_Temp_Line2(W,H);	\
																											\
		memset(ENCODER_MEMORY_GET_ADDRESS(ID_ENCODER_NewIndex_X,Mem),0,SIZE_ENCODER_NewIndex_X(W,H));		\
		memset(ENCODER_MEMORY_GET_ADDRESS(ID_ENCODER_NewIndex_Y,Mem),0,SIZE_ENCODER_NewIndex_Y(W,H));		\
																											\
		memcpy(ENCODER_MEMORY_GET_ADDRESS(ID_ENCODER_LevelQ,Mem),LevelQ_Init,SIZE_ENCODER_LevelQ(W,H));		\
																											\
		memcpy(ENCODER_MEMORY_GET_ADDRESS(ID_ENCODER_Filter_Params,Mem),                                    \
		            Filter_Params_Init,SIZE_ENCODER_Filter_Params(W,H));                                    \
//___________________________________________________________________________________________________________


#define ENCODER_MEMORY_GET_ADDRESS(ID, Mem) (( Mem + ((int*)Mem)[ID] )) 


/*=================================================================================================

					  M E M O R Y -- D E C O D E R 

=================================================================================================*/

#define	 ID_DECODER_Cnt1			0
#define	 ID_DECODER_BitLine			1
#define	 ID_DECODER_BitLine1		2	
#define	 ID_DECODER_Sign			3
#define	 ID_DECODER_Stream			4
#define	 ID_DECODER_BLIndex			5
#define	 ID_DECODER_VIndex			6
#define	 ID_DECODER_NewIndex_X		7 
#define	 ID_DECODER_NewIndex_Y		8 
#define	 ID_DECODER_QuantDelta		9 
#define	 ID_DECODER_Probability		10 
#define	 ID_DECODER_ProbLocation	11 
#define	 ID_DECODER_Quart_Buffer	12 
#define	 ID_DECODER_Temp_Line1		13 
#define	 ID_DECODER_Temp_Line2		14 
//______________________________________
#define	 ID_DECODER_END				15	

#define	 SIZE_DECODER_Cnt1(W,H)			( (((W)*(H)/2)/UBoundCNL + 1)*4 )		
#define	 SIZE_DECODER_BitLine(W,H)		( Align4((W)*(H)) )		
#define	 SIZE_DECODER_BitLine1(W,H)		( Align4(((W)*(H)/2)/8 + 1) )		
#define	 SIZE_DECODER_Sign(W,H)			( Align4(Max_(Small_Stream/8,(((W)*(H)/2)/UBoundCNL + 1)/8 + 1)) )
#define	 SIZE_DECODER_Stream(W,H)		( Align4(Small_Stream) )			
#define	 SIZE_DECODER_BLIndex(W,H)		( 256*4 )
#define	 SIZE_DECODER_VIndex(W,H)		( Small_Stream*4 )			
#define	 SIZE_DECODER_NewIndex_X(W,H)	( 0x4000 )		
#define	 SIZE_DECODER_NewIndex_Y(W,H)	( 0x4000 )		
#define	 SIZE_DECODER_QuantDelta(W,H)	( Align4(W*H) )	 		
#define	 SIZE_DECODER_Probability(W,H)	( Align4(W*H*2) )	
#define	 SIZE_DECODER_ProbLocation(W,H)	( (W*H*4+8) ) 
#define	 SIZE_DECODER_Quart_Buffer(W,H)	( Align4(W*H) )	 
#define	 SIZE_DECODER_Temp_Line1(W,H)	( Align4(W+2) )
#define	 SIZE_DECODER_Temp_Line2(W,H)	( Align4(W+2+64) )





#define	DECODER_MEMORY_SIZE(W, H) ( (ID_DECODER_END*4)					+ \
									(SIZE_DECODER_Cnt1(W,H))			+ \
									(SIZE_DECODER_BitLine(W,H))			+ \
									(SIZE_DECODER_BitLine1(W,H))		+ \
									(SIZE_DECODER_Sign(W,H))			+ \
									(SIZE_DECODER_Stream(W,H))			+ \
                    				(SIZE_DECODER_BLIndex(W,H))			+ \
									(SIZE_DECODER_VIndex(W,H))			+ \
									(SIZE_DECODER_NewIndex_X(W,H))		+ \
									(SIZE_DECODER_NewIndex_Y(W,H))		+ \
									(SIZE_DECODER_QuantDelta(W,H))	 	+ \
									(SIZE_DECODER_Probability(W,H))		+ \
									(SIZE_DECODER_ProbLocation(W,H))	+ \
									(SIZE_DECODER_Quart_Buffer(W,H))	+ \
									(SIZE_DECODER_Temp_Line1(W,H))		+ \
									(SIZE_DECODER_Temp_Line2(W,H))		) \
//_________________________________________________________________________
								


#define DECODER_MEMORY_INIT(Mem, W, H)	((int*)Mem)[0]	= ID_DECODER_END*4;									\
										((int*)Mem)[1]	= ((int*)Mem)[0]  + SIZE_DECODER_Cnt1(W,H);			\
										((int*)Mem)[2]	= ((int*)Mem)[1]  + SIZE_DECODER_BitLine(W,H);		\
										((int*)Mem)[3]	= ((int*)Mem)[2]  + SIZE_DECODER_BitLine1(W,H);		\
										((int*)Mem)[4]	= ((int*)Mem)[3]  + SIZE_DECODER_Sign(W,H);			\
										((int*)Mem)[5]	= ((int*)Mem)[4]  + SIZE_DECODER_Stream(W,H);		\
										((int*)Mem)[6]	= ((int*)Mem)[5]  + SIZE_DECODER_BLIndex(W,H);		\
										((int*)Mem)[7]	= ((int*)Mem)[6]  + SIZE_DECODER_VIndex(W,H);		\
										((int*)Mem)[8]	= ((int*)Mem)[7]  + SIZE_DECODER_NewIndex_X(W,H);	\
										((int*)Mem)[9]	= ((int*)Mem)[8]  + SIZE_DECODER_NewIndex_Y(W,H);	\
										((int*)Mem)[10] = ((int*)Mem)[9]  + SIZE_DECODER_QuantDelta(W,H);	\
										((int*)Mem)[11] = ((int*)Mem)[10] + SIZE_DECODER_Probability(W,H);	\
										((int*)Mem)[12] = ((int*)Mem)[11] + SIZE_DECODER_ProbLocation(W,H);	\
										((int*)Mem)[13] = ((int*)Mem)[12] + SIZE_DECODER_Quart_Buffer(W,H);	\
										((int*)Mem)[14] = ((int*)Mem)[13] + SIZE_DECODER_Temp_Line1(W,H);	\
																											\
		memset(DECODER_MEMORY_GET_ADDRESS(ID_DECODER_NewIndex_X,Mem),0,SIZE_DECODER_NewIndex_X(W,H));		\
		memset(DECODER_MEMORY_GET_ADDRESS(ID_DECODER_NewIndex_Y,Mem),0,SIZE_DECODER_NewIndex_Y(W,H));		\
//___________________________________________________________________________________________________________		


#define DECODER_MEMORY_GET_ADDRESS(ID, Mem) (( Mem + ((int*)Mem)[ID] )) 
